package com.hgk.common.common.ui.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

//import com.baidu.mobstat.StatService;
import com.common.dongqiuhui.apps.comm.constants.KeysConstans;
import com.common.dongqiuhui.apps.comm.constants.UrlConstans;
import com.common.dongqiuhui.apps.comm.service.BaseBallApplication;
import com.common.dongqiuhui.apps.comm.tools.BallLog;
import com.common.dongqiuhui.apps.comm.tools.BallUtil;
import com.common.dongqiuhui.apps.comm.tools.ButtomClickUtil;
import com.common.dongqiuhui.apps.comm.util.SharedPreferencesUtil;
import com.hgk.appllication.MyApplication;
import com.hgk.common.common.model.EventBusBean;
import com.hgk.common.common.ui.activity.listener.PlayingCloseListener;

import width.hgk.wanwan.R;

public class BallActivity extends FragmentActivity implements
		OnClickListener, PlayingCloseListener {

	private static final String ACTION_EXITAPP = "com.babytree.apps.lama.exit";
    public static String SIGN_URL= UrlConstans.ZWCOMMOMURL+"/user/loginbonus.json";
	/**
	 * 加载中对话框
	 */
	private ProgressDialog mLoadingDialog;

	/**
	 * 普通对话框
	 */
	public AlertDialog.Builder builder;
    /**
     * 自定义对话框
     */

    public Dialog mCustomDialog;

	protected Activity mContext;
	// 是否调用了ondestroy
	protected boolean isDestroy = false;
    private View mDialogView;

    private TextView tvSignTip1,tvSignTip2;
    private ImageView ivZaiwanGirl;
    private TextView tvSign;
    public  boolean isRequsetSign=true;
    private EventBusBean mEventBusBean;
    private RelativeLayout mRlSign;
	/**
	 * 显示普通对话框
	 * 
	 * @param title
	 *            标题
	 * @param message
	 *            内容
	 * @param view
	 *            自定义展示view 如果view!=null 则不显示message
	 * @param textLeft
	 *            左边按钮的文字
	 * @param leftListener
	 *            左边按钮监听
	 * @param textRight
	 *            右边按钮的文字
	 * @param rightListener
	 *            右边按钮的监听
	 */
	public void showAlertDialog(String title, String message, View view,
			String textLeft, DialogInterface.OnClickListener leftListener,
			String textRight, DialogInterface.OnClickListener rightListener) {
		builder = null;
		builder = new AlertDialog.Builder(this);
		if (!TextUtils.isEmpty(title)) {
			builder.setTitle(title);
		}
		if (view != null) {
			builder.setView(view);
		} else {
			if (!TextUtils.isEmpty(message))
				builder.setMessage(message);
		}

		builder.setPositiveButton(textLeft, leftListener);
		builder.setNegativeButton(textRight, rightListener);
		builder.setCancelable(true);

        builder.show();
	}

	protected void showAlertItemDialog(String title, String[] items,
			DialogInterface.OnClickListener listner) {

		builder = null;
		builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setItems(items, listner);
		builder.setCancelable(true);
		builder.show();
	}

	protected void showAlertItemDialog(String title, String[] items,
			DialogInterface.OnClickListener listner, boolean cancelable) {
		builder = null;
		builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setItems(items, listner);
		builder.setCancelable(cancelable);
		builder.show();
	}

	/**
	 * 显示加载中的对话框
	 * 
	 * @param message
	 *            提示信息
	 */
	public void showLoadingDialog(String message) {
		try {
			if (mLoadingDialog == null) {
				mLoadingDialog = new ProgressDialog(this);
			}
			mLoadingDialog.setMessage(message);
			mLoadingDialog.setCancelable(false);
			if (!mLoadingDialog.isShowing()) {
				mLoadingDialog.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showLoadingDialog(String message, boolean isCabncelable) {
		try {

			if (mLoadingDialog == null) {
				mLoadingDialog = new ProgressDialog(this);
			}
			mLoadingDialog.setMessage(message);
			mLoadingDialog.setCancelable(isCabncelable);
			if (!mLoadingDialog.isShowing()) {
				mLoadingDialog.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    public void onCreateCustomDialog(View mDialogView) {
        if (mCustomDialog == null) {
            mCustomDialog = new Dialog(this, R.style.MyDialog);
        }
        mCustomDialog.setContentView(mDialogView);
        mCustomDialog.setCanceledOnTouchOutside(true);
        mCustomDialog.show();
    }
	/**
	 * 显示加载中对话框
	 */
	public void showLoadingDialog() {
		showLoadingDialog("加载数据中...");
	}

	public void showLoadingDialog(boolean isCabncelable) {
		showLoadingDialog("加载数据中...", isCabncelable);

	}

	/**
	 * 关闭加载中的对话框
	 */
	public void closeDialog() {
		if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
			mLoadingDialog.dismiss();
		}
	}
	/**
	 * 获取用户id
	 */
	public String getUserId(){
		return TextUtils.isEmpty(SharedPreferencesUtil.getStringValue(mContext, KeysConstans.USER_ID))?
				SharedPreferencesUtil.getStringValue(mContext, KeysConstans.USER_ID):"";
	}

	/**
	 * 删除全部对话框
	 */
	protected void delDialog() {
		closeDialog();
		if (mLoadingDialog != null) {
			mLoadingDialog = null;
		}
		if (builder != null) {
			builder = null;
		}
        if(mCustomDialog!=null){
            mCustomDialog.dismiss();
            mCustomDialog=null;
        }
	}

	protected MyApplication mApplication;

	protected PlayingCloseListener babytreecloselistener;


	/**
	 * 结束activty广播
	 */
	protected BroadcastReceiver broadcastReceiverForPregnancy = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		// 设置友盟事件
		//mApplication = (LamaApplication) getApplication();
		if (babytreecloselistener != null) {
			babytreecloselistener.onCloseCreate();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		isDestroy = true;
		if (babytreecloselistener != null) {
			babytreecloselistener.onCloseDestroy();
		}
		delDialog();
	}

	@Override
	public void onClick(View v) {
		if (ButtomClickUtil.isFastDoubleClick()) {
			return;
		}

	}

	@Override
	public void onCloseCreate() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(ACTION_EXITAPP);
		this.registerReceiver(this.broadcastReceiverForPregnancy, filter);
	}

	@Override
	public void onCloseDestroy() {
		if (broadcastReceiverForPregnancy != null) {
			try {
				this.unregisterReceiver(this.broadcastReceiverForPregnancy);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	/**
	 * 关闭所有实现broadcastReceiverForPregnancy的界面
	 * 
	 * @author sam
	 */
	protected void closeOtherActivity() {
		Intent intent = new Intent();
		intent.setAction(ACTION_EXITAPP);
		sendBroadcast(intent);
	}

	@Override
	protected void onPause() {
		super.onPause();
//		StatService.onPause(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
//		StatService.onResume(this);
		if (!BaseBallApplication.isActive()) {
			BaseBallApplication.setActive(true);
			BaseBallApplication.onGoToForegroud();
           // PlayingLog.i("fyh", "light------");
           // PlayingLog.i("fyh", "isRequsetSign------="+isRequsetSign);
//            showSignDialog();
            isRequsetSign=true;
        }

	}

	@Override
	protected void onStop() {
		super.onStop();
		//LogUtil.i("ldebug", "onStop");
		if (!BallUtil.isAppOnForeground(mContext)) {
			BaseBallApplication.setActive(false);
			BaseBallApplication.onGoToBackgroud();
           // PlayingLog.i("fyh", "no  light------");

        }
	}





	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		BallLog.e("requestCode: " + requestCode + "  resultCode: "
                + resultCode + "data: " + data);
	}
    public void clearUserInfo(){
        SharedPreferencesUtil.removeKeyArray(this, KeysConstans.USER_ID, KeysConstans.NICKNAME
                , KeysConstans.HEADURI, KeysConstans.SCHOOL, KeysConstans.INTRODUCE, KeysConstans.SCHOOLCODE,
                KeysConstans.SEX, KeysConstans.TOKEN, KeysConstans.INTRODUCE, KeysConstans.ISADMIN);
    }
//    public void showSignDialog() {
//       // Toast.makeText(mContext,"isRequsetSign="+isRequsetSign,Toast.LENGTH_SHORT).show();
//        if (isRequsetSign&& !TextUtils.isEmpty(SharedPreferencesUtil.getStringValue(mContext,KeysConstans.USER_ID))) {
//            Map map = new HashMap<String,String>();
//            map.put(ApiParamsConstans.USERID, SharedPreferencesUtil.getStringValue(mContext, KeysConstans.USER_ID));
//            VolleyManager.getInstance().postString(SIGN_URL, map, new RequestListener() {
//                @Override
//                public void requestSuccess(Object result) {
//                    SignBean signBean = (SignBean) result;
//                    if (signBean != null && signBean.getData() != null) {
//                        mDialogView = LayoutInflater.from(mContext).inflate(R.layout.sign_dialog_layout, null);
//                        tvSignTip1 = (TextView) mDialogView.findViewById(R.id.tv_sign1);
//                        tvSignTip1.setText("伦家已经\n连续签到" + signBean.getData().getDays() + "天了");
//                        tvSignTip2 = (TextView) mDialogView.findViewById(R.id.tv_sign2);
//                        ivZaiwanGirl = (ImageView) mDialogView.findViewById(R.id.zaiwan_girl);
//                        mRlSign=(RelativeLayout)mDialogView.findViewById(R.id.rl_sign);
//                        if ("1".equals(signBean.getData().getType())) {
//                            tvSignTip2.setText("今天得了" + signBean.getData().getBonus() + "个玩贝啦~\n么么哒~");
//                            ivZaiwanGirl.setBackgroundResource(R.drawable.zaiwan_person_icon);
//                        } else if ("7".equals(signBean.getData().getType())) {
//                            tvSignTip2.setText("今天又送伦家" + signBean.getData().getBonus() + "个玩贝~\n哼哼哼~");
//                            ivZaiwanGirl.setBackgroundResource(R.drawable.zaiwan_person2_icon);
//                        } else {
//                            tvSignTip2.setText("今天送伦家" + signBean.getData().getBonus() + "个玩贝~\n不要不要～");
//                            ivZaiwanGirl.setBackgroundResource(R.drawable.zaiwan_person3_icon);
//                        }
//                        tvSign = (TextView) mDialogView.findViewById(R.id.tv_sign);
//                        tvSign.setOnClickListener(new OnClickListener(){
//
//                            @Override
//                            public void onClick(View v) {
//                                tvSign.setVisibility(View.GONE);
//                                mRlSign.setBackgroundResource(R.drawable.sign_instruct_icon);
//                                tvSignTip1.setVisibility(View.GONE);
//                                tvSignTip2.setVisibility(View.GONE);
//                            }
//                        });
//                        onCreateCustomDialog(mDialogView);
//                        mCustomDialog.setCanceledOnTouchOutside(true);
//                        mEventBusBean=new EventBusBean();
//                        mEventBusBean.setType(11);
//                        EventBus.getDefault().post(mEventBusBean);
//                    }
//                }
//
//                @Override
//                public void requestError(VolleyError e) {
//                    if (e.isNonetResponse()) {
//                    }
//                }
//            }, SignBean.class);
//        }
//    }
}
